<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理">
          <el-button type="primary" size="small" @click="$refs.addRole.isShow=true">新增角色</el-button>
          <br>
          <br>
          <!-- 表格 -->
          <el-table :data="list" border stripe>
            <el-table-column width="120" label="序号" type="index" />
            <el-table-column width="400" label="角色" prop="name" />
            <el-table-column width="400" label="描述" prop="description" />
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="small">权限设置</el-button>
                <el-button type="success" size="small" @click="editHandler(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="delHandler(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <br>
          <br>
          <!-- 分页 -->
          <!-- current-page 当前页码 -->
          <!-- page-sizes 每页可以有多少数量 -->
          <!-- page-size 目前每页数量 -->
          <!-- layout 布局 -->
          <!-- total 总数 -->
          <!-- size-change 每页数量切换事件 -->
          <!-- current-change 页码切换事件 -->
          <el-pagination
            :current-page="currentPage4"
            :page-sizes="[2, 3, 4, 5]"
            :page-size="queryData.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="companyData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="companyData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="companyData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="companyData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <add-role ref="addRole" @update="getRoleList" />
  </div>
</template>

<script>
import AddRole from './components/add-role.vue'
import { getRoleInfoData, getRoleListData, getCompanyInfo, delRoleData } from '@/api/setting'
export default {
  name: 'Settings',
  components: {
    AddRole
  },
  data() {
    return {
      currentPage4: 1,
      queryData: {
        page: 1,
        pagesize: 2
      },
      list: [],
      total: 0, // 总数
      companyData: {}
    }
  },
  created() {
    this.getRoleList()
    this.getCompany()
  },
  methods: {
    async editHandler(row) {
      const res = await getRoleInfoData(row.id)
      this.$refs.addRole.formData = res
      this.$refs.addRole.isShow = true
    },

    // 获取公司数据
    async getCompany() {
      const res = await getCompanyInfo(this.$store.getters.companyId)
      this.companyData = res
    },
    async getRoleList() {
      const { rows, total } = await getRoleListData(this.queryData)
      this.list = rows
      this.total = total
    },
    handleSizeChange(val) {
      console.log(`每页${val}条`)
      this.queryData.pagesize = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      console.log(`当前页${val}`)
      this.queryData.page = val
      this.getRoleList()
    },
    // 删除事件
    async delHandler(val) {
      await this.$confirm('确认删除吗', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
      await delRoleData(val.id)
      this.$message.success('删除成功')
      // 如果不是第一页，而且只有一个，删除后把页码减一
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.getRoleList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
